package com.example.wineknower.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.wineknower.R;
import com.example.wineknower.method.OnClickShare;
import com.example.wineknower.utils.OkHttpUtil;
import com.example.wineknower.utils.UserUtil;

public class FankuiActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fankui);
        setToolBar();
        Intent i=getIntent();
        String Uid = i.getStringExtra("id");

        EditText content = findViewById(R.id.content);
        EditText title = findViewById(R.id.title);
        Button feed = findViewById(R.id.feed_back_btn);
        feed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(FankuiActivity.this, "点击了提交", Toast.LENGTH_SHORT).show();
                String cont = content.getText().toString();
                String ti = title.getText().toString();
                String s = null;
                try {
                    s = OkHttpUtil.getRequest("http://212.64.93.57:8080/Feedback/addFeedback?title=" + ti + "&content="+ cont + "&userId=" + Uid);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                content.setText("");
                title.setText("");
                Toast.makeText(FankuiActivity.this, "感谢您的宝贵意见", Toast.LENGTH_SHORT).show();
            }
        });
    }
    private void setToolBar() {
        Toolbar toolbar =  findViewById(R.id.shoucang_toolbar);
        setTitleCenter(toolbar);
        this.setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        this.getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    //Toolbar的事件---返回
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
    /*
     * 设置标题居中
     */
    public void setTitleCenter(Toolbar toolbar) {
        String title = "title";
        final CharSequence originalTitle = toolbar.getTitle();
        toolbar.setTitle(title);
        for (int i = 0; i < toolbar.getChildCount(); i++) {
            View view = toolbar.getChildAt(i);
            if (view instanceof TextView) {
                TextView textView = (TextView) view;
                if (title.equals(textView.getText())) {
                    textView.setGravity(Gravity.CENTER);
                    Toolbar.LayoutParams params = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.MATCH_PARENT);
                    params.gravity = Gravity.CENTER;
                    textView.setLayoutParams(params);
                }
            }
            toolbar.setTitle(originalTitle);
        }
    }
}
